<?php
//echo" <script>alert('here0'); </script>";
	include("gen.php");
	$cmd=get_datan("cmd");
	switch($cmd){
		case 1:
		//echo" <script>alert('here1'); </script>";
			//get one vaccine based on id
			get_household();
			break;
			
		case 2:
            get_all_households();
            break;
		//echo" <script>alert('here2'); </script>";

		case 3:
            update_household();
            break;
        case 4:
            add_household();
            break;
        case 5:
            delete_household();
            break;
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
	}
	
	
	function get_household(){
		include_once("households.php");
		
		$id=$id=get_datan("id");
		$v=new households();
		$row=$v->get_household($id);
		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","household not found");
			echo "}";
			return;
		}
		
		echo "{";
			echo jsonn("result",1) .",";
			echo '"household":{';
			//id
			echo jsonn("household_id",$id).",";
			//name
			echo jsons("household_no",$row['household_no']).",";
			//scchedule
			echo jsons("household_head",$row['household_head']).",";
			
			echo jsons("discription",$row['discription']).",";
			//url
			echo jsonn("community_id",$row['community_id']);
			echo "}";
		echo "}";
	}

    function add_household()
    {

        $household_no = get_data('ahn');
        $household_head = get_data('ahh');
        $discription = get_data('adp');
        $community_id = get_data('acd');


        include("households.php");
        $v = new households();
        if(!$v-> add_household($household_no,$household_head,$discription,$community_id))
        {
            echo '{"result": 0,"message":"adding failed"}';
        }
        else
        {
            echo '{"result": 1,"message":"adding successful"}';
        }
    }
	
	function  update_household()
    {
	      $id = get_datan('id');
		   $household_no= get_data('hn');
		   $household_head= get_data('hh');
		   $discription= get_data('dp');
		   $community_id= get_data('cd');

        if(!$id)
        {
            echo "{";
            echo jsonn("result",0).",";
            echo jsons("message","id not correct");
            echo "}";
            return;
        }

           include("households.php");
		   $v = new households();
		   $row = $v-> update_household($id,$household_no,$household_head,$discription,$community_id);
		   if(!$row){
		      //return error message
			  echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","id not correct");
			echo "}";
			return;
			  }
            else
            {
                echo "{";
                echo jsonn("result",1).",";
                echo jsons("message","updated");
                echo "}";
            }
		}

    function delete_household()
    {
        $household_id=get_datan('hid');
        include_once("households.php");
        $v = new households();

        if(!$v->delete_household($household_id))
        {
               //return error message
            echo "{";
            echo jsonn("result",0). ",";
            echo jsons("message","id not correct");
            echo "}";
            return;

        }
    }
?>